package com.hitplane.elements 
{
	import com.hitplane.attack.NormalBulletAttack;
	import com.hitplane.core.Fly;
	import com.hitplane.enume.DisplayType;
	import com.hitplane.interfaces.IAttack;
	import com.hitplane.interfaces.IKey;
	import com.hitplane.manager.GameContainManager;
	import com.hitplane.manager.KeyBoardManager;
	
	import flash.ui.Keyboard;
	
	/**
	 * @describe  	普通用户飞行器
	 * @author  	郑子华
	 * @website 	http://blog.sina.com.cn/zihua2007
	 * @time		... 
	 */
	public class NormalUserFly extends Fly implements IKey
	{
		private const NORMAL_ATTACK:String = "normal_attack";
		
		public function NormalUserFly() 
		{
			setFlyState();
			setFlyAttack();
			setFlyKey();
			type = DisplayType.TYPE_USER;
			reInit();
		}
		//override==============================================================
		override public function reInit():void 
		{
			super.reInit();
			timeGap=20;
			speed = 5;
			blood = 5;
		}
		override public function onTick():void{
			var stageW:int=GameContainManager.instance.mainStage.stageWidth;
			var stageH:int=GameContainManager.instance.mainStage.stageHeight;
			var w:int=int(width/2);
			var h:int=int(height/2);
			if(x<w){
				x=w;
			}
			if(x>stageW-w){
				x=stageW-w;
			}
			if(y<h){
				y=h;
			}
			if(y>stageH-h){
				y=stageH-h;
			}
		} 
		//public===============================================================
		public function get normalAttack():String {
			return NORMAL_ATTACK;
		}
		//implements IKey===============================================================
		public function onLeft():void {
			this.x-=speed;
		}
		public function onRight():void {
			this.x+=speed;
		}
		public function onUp():void {
			this.y-=speed;
		}
		public function onDown():void {
			this.y+=speed;
		}
		public function onAttack():void{
			this.attack(NORMAL_ATTACK);
		}
		
		//private===============================================================
		private function setFlyState():void {
			registerState(flyNormalState.stateType, new NormalFlyMc());
			registerState(flyHitState.stateType, new NormalFlyHitMc());
			registerState(flyDealState.stateType, new NormalFlyDealMc());
			
			setState(flyNormalState);
		}
		private function setFlyAttack():void {
			var normalAttack:IAttack = new NormalBulletAttack(this, NormalWhitBullet,1,-1);
			normalAttack.attackType = DisplayType.TYPE_USER;
			registerAttack(NORMAL_ATTACK, normalAttack);
		}
		/**
		 * 注册按键
		 */
		private function setFlyKey():void {
			KeyBoardManager.instance.registerKey(KeyBoardManager.KEY_PRE+Keyboard.UP,onUp);
			KeyBoardManager.instance.registerKey(KeyBoardManager.KEY_PRE+Keyboard.DOWN,onDown);
			KeyBoardManager.instance.registerKey(KeyBoardManager.KEY_PRE+Keyboard.LEFT,onLeft);
			KeyBoardManager.instance.registerKey(KeyBoardManager.KEY_PRE+Keyboard.RIGHT,onRight);
			KeyBoardManager.instance.registerKey(KeyBoardManager.KEY_PRE+Keyboard.NUMPAD_1,onAttack);
		}
		
	}
	
}